<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片展示</title>
    <style>
        .gallery {
            display: flex;
            flex-wrap: wrap;  /* 允许换行 */
            justify-content: space-between;  /* 在行之间添加空间 */
        }
        .gallery div {
            flex: 0 0 calc(33.333% - 10px);  /* 每行展示 5 张图片 */
            margin-bottom: 10px;  /* 底部边距 */
        }
        .gallery img {

            max-width: 100%;  /* 确保图片宽度适应父元素 */
            height: auto;  /* 高度自动 */
            cursor: pointer; /* 添加鼠标悬停效果 */
        }
        #loadMore {
            margin: 20px;
            padding: 10px 20px;
            cursor: pointer;
        }
        .delete-button {
            display: inline-block;
            margin: 10px;
            padding: 5px 10px;
            background-color: red;
            color: white;
            border: none;
            cursor: pointer;
        }
        /* 放大图片容器样式 */
        .image-modal {
            display: none; /* 默认隐藏 */
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8); /* 半透明背景 */
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }

        .image-modal img {
            max-width: 100%;
            max-height: 100%;
/* <!--            box-shadow: 0 0 10px #fff;--> */
        }
    </style>
</head>
<body>
    <h1>图片展示</h1>
<!--       <form method="get" action="/">-->
<!--        <label for="folder">选择文件夹:</label>-->
<!--        <input type="text" id="folder" name="folder" value="{{ request.args.get('folder', '') }}">-->
<!--        <button type="submit">筛选</button>-->
<!--    </form>-->

        <!-- 文件夹选择下拉菜单 -->
    <form method="get" action="/">
        <label for="folder">选择文件夹:</label>
        <select id="folder" name="folder">
            <option value="">请选择文件夹</option>
            <!-- 遍历文件夹列表 -->
            {% for folder in folders %}
                <option value="{{ folder }}" {% if folder == selected_folder %}selected{% endif %}>{{ folder }}</option>
            {% endfor %}
        </select>
        <button type="submit">筛选</button>
    </form>

    <div class="gallery">
        <!-- 遍历图片列表并展示 -->
        {% for image in images %}
            <div>
                <!-- 缩略图展示，点击切换为原图 -->
                <img src="{{ url_for('LocalWb.send_image', filename=image) }}"
                     data-full="{{ url_for('LocalWb.send_image', filename=image) }}"
                     alt="{{ image }}" class="thumbnail">
                <!-- 删除图片的按钮 -->
                <form action="{{ url_for('LocalWb.delete_image', filename=image) }}" method="post" style="display:inline;">
                    <button type="submit" class="delete-button">删除</button>
                </form>
            </div>
        {% endfor %}
    </div>

    <!-- 放大图片显示的模态框 -->
    <div class="image-modal" id="imageModal">
        <img id="modalImage" src="" alt="放大的图片">
    </div>

    {% if total_images > (page + 1) * 10 %}
        <button id="loadMore">加载更多</button>
    {% endif %}

    <script>
        // 获取所有缩略图元素
        const images = document.querySelectorAll('.thumbnail');
        const modal = document.getElementById('imageModal'); // 模态框
        const modalImage = document.getElementById('modalImage'); // 模态框中的图片

        // 为每个图片添加点击事件
        images.forEach(img => {
            img.addEventListener('click', function() {
                modalImage.src = this.dataset.full; // 将原图加载到模态框中
                modal.style.display = 'flex'; // 显示模态框
            });
        });

        // 点击模态框关闭图片放大
        modal.addEventListener('click', function() {
            modal.style.display = 'none'; // 隐藏模态框
        });

<!--        document.getElementById('loadMore').addEventListener('click', function() {-->
<!--            let currentPage = {{ page }};-->
<!--            currentPage++;-->
<!--            window.location.href = `/?page=${currentPage}`;-->
<!--        });-->

        document.getElementById('loadMore').addEventListener('click', function() {
            let currentPage = {{ page }};
            currentPage++;
            let folder = "{{ request.args.get('folder', '') }}"; // 获取当前的文件夹参数
            let query = `/?page=${currentPage}`;
            if (folder) {
                query += `&folder=${folder}`; // 保持文件夹筛选参数
            }
            window.location.href = query;
        });
    </script>
</body>
</html>
